package com.socialsoftware.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtils {

    // Permission request code
    public static final int PERMISSION_REQUEST_CODE = 100;

    /**
     * Check if all required permissions are granted
     * @param context The context
     * @param permissions Array of permissions to check
     * @return true if all permissions are granted
     */
    public static boolean hasPermissions(Context context, String[] permissions) {
        if (context != null && permissions != null) {
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Request required permissions
     * @param activity The activity
     * @param permissions Array of permissions to request
     * @param requestCode Request code for callback
     * @return true if all permissions already granted
     */
    public static boolean requestPermissions(Activity activity, String[] permissions, int requestCode) {
        List<String> permissionsToRequest = new ArrayList<>();

        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionsToRequest.add(permission);
            }
        }

        if (!permissionsToRequest.isEmpty()) {
            ActivityCompat.requestPermissions(
                    activity,
                    permissionsToRequest.toArray(new String[0]),
                    requestCode
            );
            return false;
        }
        
        return true;
    }

    /**
     * Get required permissions for call service based on Android version
     * @return Array of permissions
     */
    public static String[] getCallServicePermissions() {
        List<String> permissions = new ArrayList<>();
        permissions.add(Manifest.permission.RECORD_AUDIO);
        permissions.add(Manifest.permission.CAMERA);
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions.add(Manifest.permission.POST_NOTIFICATIONS);
        }
        
        if (Build.VERSION.SDK_INT >= 33) {
            permissions.add(Manifest.permission.FOREGROUND_SERVICE_MICROPHONE);
            permissions.add(Manifest.permission.FOREGROUND_SERVICE_CAMERA);
        }
        
        return permissions.toArray(new String[0]);
    }
} 